//
//  TFResponderView.swift
//  TalkfunSDKDemo
//
//  Created by 莫瑞权 on 2022/5/11.
//  Copyright © 2022 Talkfun. All rights reserved.
//

import UIKit
import Lottie

class TFResponderView: TFtouchesMoved {

    @objc let backgroundView1 = UIView()
    @objc let backgroundView2 = UIView()
    @objc let backgroundView3 = UIView()
    
    @objc let responderText = UILabel()
    @objc let responderButton = UIButton()
    @objc let timeLabel = UILabel()
    // 抢答器动画
    @objc let responderAnimation = TalkfunSharedTools.getJsonDataJsonname("TF_responderAnimation")
    
    @objc let imageView = UIImageView()
    @objc let imageView2 = UIImageView()
    
    @objc let closeButton = UIButton()
    @objc let stateImage = UIImageView()
    //抢答结束 抢答成功  抢答失败
    @objc let stateILabel = UILabel()
    //用户名 或者 3S
    @objc let promptLabel_1 = UILabel()
    //用户名 或者 学员
    @objc let promptLabel_2 = UILabel()
    //抢中
    @objc let promptLabel_3 = UILabel()
    //抢答结束的UI;
    @objc let promptLabel_4 = UILabel()

 
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.backgroundView1.backgroundColor = UIColor(red: 28/255.0, green: 31/255.0, blue: 35/255.0, alpha: 0.98)
        self.addSubview(backgroundView1)
        
   
        self.responderAnimation.isHidden = true
        self.backgroundView1.addSubview(self.responderAnimation)
        

 
        self.responderButton.backgroundColor = UIColor(red: 77/255.0, green: 154/255.0, blue: 255/255.0, alpha: 1)
        self.responderButton.layer.masksToBounds=true
        self.backgroundView1.addSubview(self.responderButton)
        self.responderText.layer.masksToBounds=true

       
        self.responderText.textAlignment = .center
        
        self.responderText.font = UIFont.PingFang(size: 20)
        self.responderText.numberOfLines = 0
        self.responderText.adjustsFontSizeToFitWidth = true
        self.responderText.minimumScaleFactor = 10
        
        self.responderText.text = "计时结束"
        self.responderText.textColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1)
        
        self.responderText.backgroundColor = UIColor(red: 77/255.0, green: 154/255.0, blue: 255/255.0, alpha: 1)
        self.backgroundView1.addSubview(self.responderText)

        self.timeLabel.numberOfLines = 0
        self.backgroundView1.addSubview(self.timeLabel)
       
        self.timeLabel.font =  UIFont.PingFang(size: 15)
        self.timeLabel.text = "00:00"
        timeLabel.textAlignment =  .center
        timeLabel.textColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1)

//------------------------中与不中------------------------------------------------
        self.backgroundView2.isHidden = true
        
        self.backgroundView2.backgroundColor = UIColor(red: 28/255.0, green: 31/255.0, blue: 35/255.0, alpha: 0.98)
        self.addSubview(self.backgroundView2)
        
        self.backgroundView2.addSubview(self.stateImage)

        self.stateILabel.numberOfLines = 0
        self.backgroundView2.addSubview(self.stateILabel)
        self.stateILabel.textColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1)
        self.stateILabel.font = UIFont.PingFang(size: 18)
//        self.stateILabel.text = @"抢答成功";
        self.stateILabel.textAlignment = .center
        self.stateILabel.textColor = UIColor(red: 77/255.0, green: 154/255.0, blue: 255/255.0, alpha: 1)

        self.promptLabel_1.numberOfLines = 0;
        self.promptLabel_1.textAlignment =  .center
        self.backgroundView2.addSubview(self.promptLabel_1)
        self.promptLabel_1.font = UIFont.PingFang(size: 14)
        self.promptLabel_1.textColor = UIColor(red: 77/255.0, green: 154/255.0, blue: 255/255.0, alpha: 1)
        self.promptLabel_1.isHidden = true

        self.promptLabel_2.numberOfLines = 0;
        self.backgroundView2.addSubview(self.promptLabel_2)
        self.promptLabel_2.font = UIFont.PingFang(size: 14)
         self.promptLabel_2.textColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1)
        self.promptLabel_2.textAlignment = .right
        

 
        self.promptLabel_3.numberOfLines = 0;
        self.backgroundView2.addSubview(self.promptLabel_3)
        self.promptLabel_3.font = UIFont.PingFang(size: 14)
        self.promptLabel_3.textColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1)
//        promptLabel_3.text = @"抢中";
        self.promptLabel_3.textAlignment = .left;
        
        self.promptLabel_2.isHidden = true
        self.promptLabel_3.isHidden = true
   

        self.promptLabel_4.numberOfLines = 0;
        self.backgroundView2.addSubview(self.promptLabel_4)
        self.promptLabel_4.font = UIFont.PingFang(size: 14)
        self.promptLabel_4.textColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1)
        self.promptLabel_4.textAlignment = .center
       
        self.imageView2.image =  UIImage.init(named: "HTLive.bundle/关掉抢答器")
        self.addSubview(self.imageView2)
        self.closeButton.setImage(UIImage.init(named: "HTLive.bundle/关掉抢答器"), for: .normal)
        self.closeButton.addTarget(self, action: #selector(closeButtonClict), for: .touchUpInside)
        self.addSubview(self.closeButton)
        
    }
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    @objc func closeButtonClict()
    {
        self.isHidden = true
        self.timeLabel.text = "00:00"
    }
    //成功抢答
    @objc func setResponder(_ name:String,duration:NSInteger,xid:String)

    {
        self.backgroundView1.isHidden = true
        self.backgroundView2.isHidden = false
        
        self.stateImage.image = UIImage.init(named: "HTLive.bundle/responderSuccess")

        self.stateILabel.textColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1)
        
        self.stateILabel.text = "抢答成功"

        self.promptLabel_2.textColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1)
        
        
        self.promptLabel_2.text = "花了"
        
        self.promptLabel_1.text = String.init(format: "%li", duration)+"s"
        self.promptLabel_1.textColor = UIColor(red: 77/255.0, green: 154/255.0, blue: 255/255.0, alpha: 1)
        
        self.promptLabel_3.text = "抢到"
        
        self.promptLabel_3.textColor =   UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1)
        

        let second:String = String.init(format: "%li", duration)
        
        let s:String = "花了 %ds 抢到"
        
        let allstr = String.init(format: s, duration)
        
        let isOn:Bool =  TalkfunSettingToolsManager.shared().getToolsLanguageChange(xid)
        
        
        var mStr:NSMutableAttributedString = NSMutableAttributedString.init(string: allstr)
        
        if (isOn) {
            mStr = NSMutableAttributedString.getAttributed(string: allstr, font: UIFont.PingFang(size: 14), foregroundColor: UIColor.white)
            
            mStr.addAttribute(.foregroundColor, value: UIColor(red: 77/255.0, green: 154/255.0, blue: 255/255.0, alpha: 1.0), range: NSRange(location: 5, length: second.count))
           
        } else {
            
            mStr = NSMutableAttributedString.getAttributed(string: allstr , font: UIFont.PingFang(size: 14), foregroundColor: UIColor.white)
            
            mStr.addAttribute(.foregroundColor, value: UIColor(red: 77/255.0, green: 154/255.0, blue: 255/255.0, alpha: 1.0), range: NSRange(location: 2, length: name.count))
          
        }
        
       
        
        self.promptLabel_4.attributedText = mStr
        
        self.responderAnimation.isHidden = true
        
        self.layoutSubviewsUI()
    }

    //抢失败
    @objc func fail(_ name:String,xid:String)
    {
         self.backgroundView1.isHidden = true
         self.backgroundView2.isHidden = false
        
         self.stateImage.image = UIImage.init(named: "HTLive.bundle/responderFailure")
        
         self.stateILabel.textColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1)
        
        self.stateILabel.text = "抢答失败"
        
         self.promptLabel_2.textColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1)
        
        self.promptLabel_2.text = "by"
        
        
         self.promptLabel_1.text = name;
        
         self.promptLabel_1.textColor = UIColor(red: 77/255.0, green: 154/255.0, blue: 255/255.0, alpha: 1)
        
        self.promptLabel_3.text = "抢中"
        self.promptLabel_3.textColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1)
        
        let s = "被 %@ 抢中"
        
        let allstr = String.init(format: s, name)
       
        let isOn:Bool =  TalkfunSettingToolsManager.shared().getToolsLanguageChange(xid)
        
        
        var mStr:NSMutableAttributedString = NSMutableAttributedString.init(string: allstr)
        if (isOn) {
            mStr = NSMutableAttributedString.getAttributed(string: allstr , font: UIFont.PingFang(size: 14), foregroundColor: UIColor(red: 77/255.0, green: 154/255.0, blue: 255/255.0, alpha: 1))
          
        } else {
            
            mStr = NSMutableAttributedString.getAttributed(string: allstr , font: UIFont.PingFang(size: 14), foregroundColor: UIColor.white)
            
            mStr.addAttribute(.foregroundColor, value: UIColor(red: 77/255.0, green: 154/255.0, blue: 255/255.0, alpha: 1.0), range: NSRange(location: 2, length: name.count))
          
        }
        

        self.promptLabel_4.attributedText = mStr
        
        self.responderAnimation.isHidden = true
        
        self.layoutSubviewsUI()
    }
    //没人作答
    @objc func noOne()

    {
         self.backgroundView1.isHidden = true
         self.backgroundView2.isHidden = false
        
         self.stateImage.image = UIImage.init(named: "HTLive.bundle/responderFinish")
        
         self.stateILabel.textColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1)
        
        self.stateILabel.text = "抢答结束"
        
         self.promptLabel_2.textColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1)
         self.promptLabel_2.text = ""
        
        
        self.promptLabel_1.text = "无人抢答"
         self.promptLabel_1.textColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1)
        self.promptLabel_3.text = ""
        self.promptLabel_3.textColor =  UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1)
        
        self.promptLabel_4.text = "无人抢答"
        self.promptLabel_4.textColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1)
        self.layoutSubviewsUI()
      
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        self.layoutSubviewsUI()
    }
    @objc func layoutSubviewsUI()
    {
        self.backgroundView1.frame = self.bounds
        self.backgroundView2.frame = self.bounds
        self.backgroundView1.layer.cornerRadius = 10
        self.backgroundView2.layer.cornerRadius = 10

        self.timeLabel.frame = CGRect(x: 0, y: 24.5, width: self.frame.size.width, height: 21)
  
        self.responderAnimation.frame = CGRect(x: (self.frame.size.width-115)/2, y: self.timeLabel.frame.maxY, width: 115, height: 115)
       
        self.responderButton.frame = CGRect(x: (self.frame.size.width-80)/2, y: self.timeLabel.frame.maxY+17.5, width: 80, height: 80)
 
        self.responderButton.layer.cornerRadius = self.responderButton.frame.size.width/2
        self.responderText.frame = self.responderButton.frame
        self.responderText.layer.cornerRadius = self.responderText.frame.size.width/2

        self.imageView.frame = CGRect(x: self.frame.size.width - 30, y: 0, width: 30, height: 30)
      
        self.closeButton.frame = CGRect(x: self.frame.size.width - 50, y: 0, width: 50, height: 50)
 
        
        self.stateImage.frame = CGRect(x: (self.frame.size.width-60)/2, y: 92.5-5, width: 60, height: 60)
        
        self.stateILabel.frame = CGRect(x: 0, y: 26, width: self.frame.size.width, height: 25)
       
        
        
            let myString = "Momo酱"
            let attrsSize: CGSize = myString.size(withAttributes: [NSAttributedString.Key.font: UIFont.PingFang(size: 14)])

        
        var promptLabel_1Text:String = ""
        if self.promptLabel_1.text != nil {
            promptLabel_1Text = self.promptLabel_1.text!
        }
        
        let attrsSize1:CGSize = promptLabel_1Text.size(withAttributes: [NSAttributedString.Key.font: UIFont.PingFang(size: 14)])
        
        var width = attrsSize1.width
        if (attrsSize1.width > attrsSize.width) {
            width = attrsSize.width+4
        }
        
        self.promptLabel_1.frame = CGRect(x: (self.frame.size.width-width)/2, y: self.stateILabel.frame.maxY+7, width: width, height: 20)
     
        
        self.promptLabel_2.frame = CGRect(x: 0, y: self.stateILabel.frame.maxY+7, width: self.promptLabel_1.frame.origin.x-5, height: 20)
        
        self.promptLabel_3.frame = CGRect(x: self.promptLabel_1.frame.maxX+5, y: self.stateILabel.frame.maxY+7, width: self.frame.size.width-self.promptLabel_1.frame.maxX-5, height: 20)
        self.promptLabel_4.frame = CGRect(x: 0, y: self.stateILabel.frame.maxY+7, width: self.frame.size.width, height: 20)
        
    }
    //配置总时长
    @objc func responderStartWidthDuration(_ duration:NSInteger)

    {

      if (self.isHidden) {
            return
        }
      
        self.timeLabel.font = UIFont.PingFang(size: 15)
              
        self.timeLabel.text = (TalkfunSharedTools.minutesSndSeconds(duration) as String)
        self.responderAnimation.isHidden = false
        
    }
    //切换语言
    @objc func upDataLanguage()
    {
        
        self.responderText.text = "计时结束"
    }

}
